package com.leo.collisions;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.dsl.components.EffectComponent;
import com.almasb.fxgl.dsl.components.HealthIntComponent;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.physics.CollisionHandler;
import com.leo.TankApp;
import com.leo.common.GameType;
import com.leo.effects.HelmetEffect;

/**
 * 子弹和玩家的碰撞检测
 */
public class BulletPlayerHandler extends CollisionHandler {

    public BulletPlayerHandler() {
        super(GameType.BULLET, GameType.PLAYER);
    }

    @Override
    protected void onCollisionBegin(Entity a, Entity b) {
        // 如果有无敌特效，子弹不生效
        boolean helmetEffect = b.getComponent(EffectComponent.class).hasEffect(HelmetEffect.class);
        if (helmetEffect) {
            a.removeFromWorld();
            return;
        }
        // 生命进度条
        HealthIntComponent healthIntComponent = b.getComponent(HealthIntComponent.class);
        // 每被击中一次减少一格血
        healthIntComponent.damage(1);
        // 血槽为零则移除玩家
        if (healthIntComponent.isZero()) {
            b.removeFromWorld();
            FXGL.spawn("explode", b.getCenter().subtract(50 / 2.0, 50 / 2.0));
            FXGL.<TankApp>getAppCast().gameOver();
        }
        a.removeFromWorld();
    }
}
